//
//  ViewController.m
//  212--Homework  QQy音乐  阿里星球
//
//  Created by 冀永金 on 16/8/12.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"
#import "FirstModel.h"
#import "SecondModel.h"
#import "ThirdModel.h"
#import "HeadView.h"
#import "JYYNetWorkParser.h"
#import "SongTableViewCell.h"
#import "NewSongTableViewCell.h"
#import "PhoneSongTableViewCell.h"
#import "RecommendTableViewCell.h"
#import "ExclusiveTableViewCell.h"
#import "HotTableviewCell.h"
#define WIDTH  self.view.bounds.size.width
#define HEIGHT   self.view.bounds.size.height
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableViewOfMusic;
@property (nonatomic, strong) FirstModel *first;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.backgroundColor = [UIColor blackColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:nil];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"直播",@"音乐"]];
    self.navigationItem.titleView = segment;
    segment.tintColor = [UIColor colorWithRed:0.69 green:0.58 blue:0.84 alpha:1.00];
    segment.layer.backgroundColor = [UIColor whiteColor].CGColor;
    segment.frame = CGRectMake(105, 0, 120, 30);
    segment.layer.cornerRadius = 15;
    segment.clipsToBounds = YES;
    [self creaTabelView];
    [self handleData];
    [self createHeardView];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)createHeardView {
    HeadView *head = [[HeadView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, 300)];
    self.tableViewOfMusic.tableHeaderView = head;
    //head.backgroundColor = [UIColor redColor];
    
}
- (void)creaTabelView {
    self.tableViewOfMusic = [[UITableView alloc]initWithFrame:CGRectMake(0, 64,WIDTH , HEIGHT - 64 - 49) style:UITableViewStylePlain];
    [self.view addSubview:_tableViewOfMusic];
    _tableViewOfMusic.backgroundColor = [UIColor whiteColor];
    self.tableViewOfMusic.delegate = self;
    self.tableViewOfMusic.dataSource =self;
    [_tableViewOfMusic registerClass:[SongTableViewCell class] forCellReuseIdentifier:@"Song"];
    [_tableViewOfMusic registerClass:[NewSongTableViewCell class] forCellReuseIdentifier:@"poolOfNewSong"];
    [_tableViewOfMusic registerClass:[PhoneSongTableViewCell class] forCellReuseIdentifier:@"PhoneSong"];
    [_tableViewOfMusic registerClass:[RecommendTableViewCell class] forCellReuseIdentifier:@"Recommend"];
    [_tableViewOfMusic registerClass:[ExclusiveTableViewCell class] forCellReuseIdentifier:@"Exclusive"];
    [_tableViewOfMusic registerClass:[HotTableviewCell class] forCellReuseIdentifier:@"Hot"];
    _tableViewOfMusic.showsVerticalScrollIndicator = NO;
}
- (void)handleData {
    [JYYNetWorkParser GETWithURL:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1470998222737&app=ttpod&v=v9.0.4.2016070616&user_id=0&mid=iPhone8%2C1&f=f320&s=s310&imsi=&hid=&splus=10.0&active=1&net=2&openudid=4bbd8925c4a3991ee51c9cd8b9a8dc5961820764&idfa=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&utdid=V62kivWPJBYDABK%2FL8gCUnDI&alf=(null)&bundle_id=com.ttpod.music&latitude=(null)&longtitude=(null)&access_token=(null)&vc=9000400" par:nil success:^(id responseObject) {
        self.first = [FirstModel modelWithDic:responseObject];
        [self.tableViewOfMusic reloadData];
    } filed:^(NSError *error) {
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.first.data.count - 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        SongTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Song" forIndexPath:indexPath];
        SecondModel *second = self.first.data[2];
        cell.bigLabel.text = second.name;
        cell.backgroundColor = [UIColor colorWithRed:0.08 green:0.11 blue:0.13 alpha:1.00];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }  else if (indexPath.row == 1) {
        NewSongTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolOfNewSong" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithRed:0.07 green:0.10 blue:0.12 alpha:1.00];
        SecondModel *second = self.first.data[3];
        cell.titleLabel.text = second.name;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }else if (indexPath.row == 2 ) {
        PhoneSongTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PhoneSong" forIndexPath:indexPath];
        SecondModel *second = self.first.data[4];
        cell.titleLabel.text = second.name;
         cell.backgroundColor = [UIColor colorWithRed:0.07 green:0.10 blue:0.12 alpha:1.00];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.row == 3) {
        HotTableviewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"Hot" forIndexPath:indexPath];
        SecondModel *second = self.first.data[5];
        cell.titleLabel.text = second.name;
        cell.backgroundColor = [UIColor colorWithRed:0.07 green:0.10 blue:0.12 alpha:1.00];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;

    }
    
    else if (indexPath.row == 4) {
        RecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Recommend" forIndexPath:indexPath];
        SecondModel *second = self.first.data[6];
        cell.titleLabel.text = second.name;
        cell.backgroundColor = [UIColor colorWithRed:0.07 green:0.10 blue:0.12 alpha:1.00];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else {
        ExclusiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Exclusive" forIndexPath:indexPath];
        SecondModel *second = self.first.data[7];
        cell.titleLabel.text = second.name;
        cell.backgroundColor = [UIColor colorWithRed:0.07 green:0.10 blue:0.12 alpha:1.00];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0 || indexPath.row == 4) {
        return 200;
        
    }else if (indexPath.row == 1) {
        return 280;
    }else if (indexPath.row == 2 || indexPath.row == 3) {
        return 480;
    }else {
        return 700;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
